var codes = "";
var homeArea = "";
var preElectricFee ="";
var openId = "";
var beforeElec = "";
var nowTolalElec = "";
var jieyuMoney = "";
var electricFee = "";
var jieyuMoneySS = "";//第二次计算的结余金额
var electricNumber = "";//房屋编号
var moneyMin = "";//本次用电量的所需金额
$(function(e){
	receiveData();
	dataInit();
});

function dataInit(){
	codes = $.req("code");
	AjaxPostUtil.request({url : path+"1853f065ae4845f2a235af6449c18767", params:{codes:codes}, type : 'json', callback : function(json){
		if(json.returnCode == '-999'){//重新登记
			openId = json.bean.openId;
			location.href='../../html/wechat_signup/wechat_signup.html?openId='+openId;
		}else if(json.returnCode == '-888'){
			//关闭当前页面，返回到微信主页面
			swal({ title : "提示", text : "信息正在审核中...", type : "warning" ,closeOnConfirm:false, closeOnCancel:false },function(){
				wx.closeWindow();
			});
		}else if(json.returnCode == '-777'){
			swal({ title : "提示", text : "信息审核失败，请重新登记！", type : "warning", closeOnConfirm:false, closeOnCancel:false}, function(){
				openId = json.bean.openId;
				location.href='../../html/wechat_signup/wechat_signup.html?openId='+openId;
			});
		}else if(json.returnCode == 0){//审核成功
			$("#houseNum").val(json.bean.hInfo);
			$("#familyPhone").val(json.bean.familyPhone);
			$("#familyName").val(json.bean.familyName);
			electricNumber = json.bean.electricNumber;
			//电费表的房屋编号
			var params = {
					electricNumber : electricNumber
			}
			//查询电费信息
			AjaxPostUtil.request({url : path+"4f1b3cbdf4c743969ad6e67bf9910829", params : params, type : 'json', callback : function(jsonMation){
	    		if(jsonMation.returnCode == 0){
	    			console.log(jsonMation);
	    			var nowDate = new Date();
	    			$("#nowTime").val(jsonMation.bean.nowTime);//获取当前时间
	    			beforeElec = jsonMation.bean.beforeElec;
	    			$("#beforeElec").val(beforeElec);
	    			$("#nowElec").val(jsonMation.bean.nowElec);
	    			nowTolalElec = jsonMation.bean.nowTolalElec;//用电量
	    			moneyMin = (nowTolalElec * 0.56).toFixed(2);
	    			$("#nowTolalElec").val("本次用电量:"+nowTolalElec);
	    			jieyuMoney = jsonMation.bean.JieyuMoney;
	    			$("#jieyuMoney").val("结余金额:"+jieyuMoney);
	    			electricFee = (nowTolalElec * 0.56 - jieyuMoney).toFixed(2);//应交电费
	    			$("#electricFee").val("应交金额:"+electricFee);
	    			$("#electricModal").removeAttr("style");
	    		}else{
	    			swal({ title : "警告", text : "暂无缴费信息，请联系物业！", type : "warning" },function(){
	    				wx.closeWindow();
	    			}); 
	    		}
	    	}});
			
		}else{
			swal({ title : "警告", text : "请重新打开页面", type : "warning" ,closeOnConfirm:false, closeOnCancel:false },function(){
				wx.closeWindow();
			});
		}
	}});
	eventInit();
	
}


function eventInit(){
	//提交订单
	$('body').on('click', '#addBtn', function(e){
		
		var params = {
				/*preElectricFee : $("#preElectricFee").val(),*/
				preElectricFee : 0.01,
		}
		
		AjaxPostUtil.request({url : path+"fed613263b0a4f03bdc41ad2264b4c53", params : params, type : 'json', callback : function(json){
    		if(json.returnCode == 0){
    			console.log(json);
    			var paySign = json.bean.paySign;
    			var appId = json.bean.appId;
    			var timeStamp = json.bean.timestamp;
    			var nonceStr = json.bean.noncestr;
    			var paypackage = json.bean.paypackage;
    			var signature = json.bean.signature;
    			var orderNumber = json.bean.orderNumber;
    			var orderId = json.bean.orderId;
    			payEvent(paySign,appId,timeStamp,nonceStr,paypackage,signature,orderNumber,orderId);
    		}else{
    			swal({ title : "错误", text : "参数错误", type : "error" }); 
    		}
    	}});
	});
	
	$('body').on('change', '#preElectricFee', function(e){
		var preElectricMoney= $("#preElectricFee").val();
		var electricMoney = electricFee;
		var jieyu = jieyuMoney;
		var num = (preElectricMoney - electricMoney).toFixed(2);//预交电费-应交电费
		if(num < 0){
			$("#preElectricFee").val("");
			swal({ title : "警告", text : "预交电费不能低于应交电费金额", type : "warning" }); 
		}else{
			$("#jieyuMoney").val("结余金额："+num);
			jieyuMoneySS = num;
		}
	});
}

//支付事件
function payEvent(paySign,appId,timeStamp,nonceStr,paypackage,signature,orderNumber,orderId){
	 wx.config({  
	    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。  
	    appId: appId, // 必填，公众号的唯一标识  
	    timestamp: timeStamp, // 必填，生成签名的时间戳  
	    nonceStr: nonceStr, // 必填，生成签名的随机串  
	    signature: signature,// 必填，签名，见附录1  
	    jsApiList: ['chooseWXPay','onMenuShareTimeline','onMenuShareAppMessage','showOptionMenu'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2  
	}); 
	 wx.ready(function(){
		 wx.chooseWXPay({  
			timestamp: timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符  
			nonceStr: nonceStr, // 支付签名随机串，不长于 32 位  
			package: paypackage, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）  
			signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'  
			paySign: paySign, // 支付签名  
			success:function(res){
				var myParams = {
						orderNumber:orderNumber,
						nowDate : $("#nowTime").val(),//缴费时间
						moneyMin : moneyMin,//本次用电的应交金额
						jieyuMoneySS : jieyuMoneySS,//第二次计算的结余金额
						totalQianMoney : 0,//欠费金额
						preElectricFee : $("#preElectricFee").val(),//预交电费
						electricNumber : electricNumber//房号
				}
				AjaxPostUtil.request({url:path+"896b256917c84b5d900f512002524de9",params:myParams,type:'json',callback:function(json){
					if(json.returnCode == 0){
//						location.href="moneySuccess.html?orderNumber="+orderNumber+"&orderId="+orderId;
//						swal({ title : "支付成功", text : "", type : "success" }); 
						wx.closeWindow();
					}else{
						swal({ title : "错误", text : "支付失败，请联系物业客服！", type : "error" }); 
					}
				}});
			},
			fail:function(res){
				swal({ title : "支付失败", text : "", type : "error" }); 
			},
			complete:function(res){
				//唤醒支付按钮
				$("#addBtn").attr("disabled", false); 
				$("#addBtn").css("background", "#03bb9b"); 
	        }
		});  
	 });
}
